﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KCMS.BusinessLogic.Interfaces;
using StructureMap;
using KCMS.BusinessLogic.Models;
using KCMS.Areas.Admin.Engine;
using KCMS.Areas.Admin.Models;
namespace KCMS.Areas.Admin.Controllers
{
    public class AllowableProductSizeController : Controller
    {
        private IAllowableProductSizeService allowSizeService = ObjectFactory.GetInstance<IAllowableProductSizeService>();
        private IProductService productService = ObjectFactory.GetInstance<IProductService>();
        private GenderListGenerator<Size> sizeListGenerator = new GenderListGenerator<Size>();
        private ISizeService sizeService = ObjectFactory.GetInstance<ISizeService>();
        public ActionResult Index(Guid productId)
        {
            var model = allowSizeService.getByProduct(productId);
            var product = productService.getById(productId);
            ViewBag.Product = product.Article + " "
                          + product.Title + " "
                          + product.Brend.Title;
            ViewBag.ProductId = productId;
            return View(model);
        }
        [HttpGet]
        public ActionResult Create(Guid productId)
        {
            AllowableProductSizeEditorModel allowSize = new AllowableProductSizeEditorModel
            {
                AllowableProductSize = new AllowableProductSize
                {
                    ProductId = productId

                },
                SizeList = sizeListGenerator.getGenderList(sizeService.getAll())
            };
            return View(allowSize);
        }
        [HttpPost]
        public ActionResult Create(AllowableProductSizeEditorModel model)
        {
            allowSizeService.save(model.AllowableProductSize);
            return RedirectToAction("Index", new { productId = model.AllowableProductSize.ProductId });
        }
        [HttpGet]
        public ActionResult Edit(Guid id)
        {
            AllowableProductSizeEditorModel allowSize = new AllowableProductSizeEditorModel
            {
                AllowableProductSize = allowSizeService.getById(id),
                SizeList = sizeListGenerator.getGenderList(sizeService.getAll())
            };
            return View(allowSize);
        }
        [HttpPost]
        public ActionResult Edit(AllowableProductSizeEditorModel model)
        {
            allowSizeService.update(model.AllowableProductSize);
            return RedirectToAction("Index", new { productId = model.AllowableProductSize.ProductId });
        }
        [HttpGet]
        public ActionResult Delete(Guid id)
        {
            var model = allowSizeService.getById(id);
            allowSizeService.delete(id);
            return RedirectToAction("Index", new { productId = model.ProductId });
        }
        [HttpGet]
        public ActionResult Choose(Guid productId)
        {
            var product = productService.getById(productId);
            var sizes = sizeService.getAll();
            IList<AllowableProductSize> list = new List<AllowableProductSize>(sizes.Count());
            foreach (var item in sizes)
            {
                list.Add(new AllowableProductSize
                {
                    ProductId=productId,
                    SizeId=item.Id,
                    Size=item,
                    Product=product
                });
            }
            return View(list);
        }
        [HttpPost]
        public void Choose(Guid [] Sizes,Guid ProductId)
        {
            foreach (Guid sizeId in Sizes)
            {
                allowSizeService.save(new AllowableProductSize
                {
                    SizeId=sizeId,
                    ProductId=ProductId
                });
            }
            return;
        }
    }
}
